package org.yanfengying.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.yanfengying.org.domain.Department;
import org.yanfengying.org.mapper.DepartmentMapper;
import org.yanfengying.org.service.IDepartmentService;
import org.yanfengying.utils.service.impl.BastServerImpl;

import java.util.Date;
import java.util.List;

@Service

public class DepartmentServiceImpl extends BastServerImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void insert(Department department) {
        String path = "";
        department.setCreate_time(new Date());
        super.insert(department);
        //处理path->先新增,便于拿到自己的id,然后根据父部门的id拿到父部门的path和自己的path拼接
        if(department.getParent().getId() != null){
            //父部门不等于空的时候就查询父部门的path进行拼接
            Department department1 = departmentMapper.loadById(department.getParent().getId());
            //拼接
            path = department1.getPath()+"/"+department.getId();
        }else {
            path = "/"+department.getId();
        }
        department.setPath(path);
        super.update(department);
    }

    @Override
    public void update(Department department) {
        String path = "";
        department.setUpdate_time(new Date());
        if(department.getParent().getId() != null){
            //父部门不等于空的时候就查询父部门的path进行拼接
            Department department1 = departmentMapper.loadById(department.getParent().getId());
            //拼接
            path = department1.getPath()+"/"+department.getId();
        }else {
            path = "/"+department.getId();
        }
        department.setPath(path);
        super.update(department);
    }

    @Override
    public List<Department> loadTreep() {
        return departmentMapper.loadTree();
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void batchDel(List<Long> ids) {
        departmentMapper.batchDel(ids);
    }
}
